package com.example.webfluxdemo;

import org.springframework.http.server.reactive.ServerHttpRequest;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * Created by victor on 2021/10/20 10:18.
 */
public class HttpContext {
    public static final Class<ServerHttpRequest> CONTEXT_KEY = ServerHttpRequest.class;

    public static Mono<Boolean> isDebug() {
        return Mono.deferContextual(ctx -> Mono.just(ctx.get("isDebug")));
    }

    public static Mono<Map<String, String>> elk() {
        return Mono.deferContextual(ctx -> Mono.just(ctx.get("elk")));
    }

    public static Mono<Void> appendElkLog(String key, String value) {
        Mono<Map<String, String>> mapMono = Mono.deferContextual(ctx -> Mono.just(ctx.get("elk")));
        return mapMono.mapNotNull(map -> map.put(key, value)).then();
    }
}
